import { BaseModel } from './BaseModel.js';

export class CourseModel extends BaseModel {
  constructor() {
    const defaultCourses = [
      {
        id: 1,
        name: "企业级人工智能项目实训一",
        code: "AI001",
        teacher: "曲凤才",
        students: 30,
        duration: "2023-09-01 至 2024-01-15",
        status: "ongoing",
        credit: 4,
        hours: 64,
        teacherId: 2
      },
      {
        id: 2,
        name: "企业级人工智能项目实训二",
        code: "AI002", 
        teacher: "曲凤才",
        students: 25,
        duration: "2024-02-20 至 2024-06-30",
        status: "upcoming",
        credit: 4,
        hours: 64,
        teacherId: 2
      },
      {
        id: 3,
        name: "Python程序设计",
        code: "PY001",
        teacher: "张老师",
        students: 40,
        duration: "2023-09-01 至 2024-01-15",
        status: "ongoing",
        credit: 3,
        hours: 48,
        teacherId: 1
      },
      {
        id: 4,
        name: "数据结构",
        code: "CS001",
        teacher: "张老师",
        students: 35,
        duration: "2023-09-01 至 2024-01-15",
        status: "ongoing",
        credit: 3,
        hours: 48,
        teacherId: 1
      },
      {
        id: 5,
        name: "软件工程导论",
        code: "SE001",
        teacher: "李老师",
        students: 32,
        duration: "2023-09-01 至 2024-01-15",
        status: "ongoing",
        credit: 2,
        hours: 32,
        teacherId: 3
      }
    ];
    super('courses', defaultCourses);
  }

  // 根据课程代码查找课程
  findByCode(code) {
    return this.findOne({ code });
  }

  // 根据教师查找课程
  findByTeacher(teacher) {
    const data = this.findAll();
    return data.filter(course => course.teacher === teacher);
  }

  // 根据教师ID查找课程
  findByTeacherId(teacherId) {
    const data = this.findAll();
    return data.filter(course => course.teacherId === teacherId);
  }

  // 获取进行中的课程
  findOngoingCourses() {
    const data = this.findAll();
    return data.filter(course => course.status === 'ongoing');
  }

  // 获取即将开始的课程
  findUpcomingCourses() {
    const data = this.findAll();
    return data.filter(course => course.status === 'upcoming');
  }

  // 更新课程学生数量
  updateStudentCount(courseId, studentCount) {
    return this.update(courseId, { students: studentCount });
  }

  // 根据课程名称查找
  findByName(name) {
    const data = this.findAll();
    return data.find(course => course.name === name);
  }

  // 搜索课程
  searchCourses(keyword) {
    const data = this.findAll();
    const lowerKeyword = keyword.toLowerCase();
    return data.filter(course => 
      course.name.toLowerCase().includes(lowerKeyword) ||
      course.code.toLowerCase().includes(lowerKeyword) ||
      course.teacher.toLowerCase().includes(lowerKeyword)
    );
  }
}